using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ruminate.GUI.Framework;
using Ruminate.Utils;

namespace Ruminate.GUI.Content
{
    /// <summary>
    /// 
    /// </summary>
    public class ToggleButton : Element {

        /*####################################################################*/
        /*                               Variables                            */
        /*####################################################################*/

        /* ## Local Variables ## */
        private SlidingDoorRenderer _slidingDoorRenderer;
        private TextRenderer _textRenderer;
        private Texture2D _text, _default, _hover, _pressed;

        /* ## Exposed Variables ## */
        public string Label { get; set; }    
    
        public ElementEvent OnToggle { get; set; }
        public ElementEvent OffToggle { get; set; }

        private bool _innerIsToggled;
        public bool IsToggled {
            get { return _innerIsToggled; }
            set {
                if (value != _innerIsToggled) {
                    if (value) {
                        if (OnToggle != null) { OnToggle(this); }
                    } else {
                        if (OffToggle != null) { OffToggle(this); }
                    }
                }

                _innerIsToggled = value;
            }
        } 

        /*####################################################################*/
        /*                        Node Initialization                         */
        /*####################################################################*/

        public ToggleButton(int x, int y, string label) 
            : base(new Offset(x, y, 0, 0)) {

            _innerIsToggled = false;
            Label = label;
        }

        /*####################################################################*/
        /*                      Widget  Initialization                        */
        /*####################################################################*/

        protected internal override void Init() {

            _slidingDoorRenderer = (SlidingDoorRenderer)InstanceData.DefaultRenderers["Button"];
            _textRenderer = (TextRenderer)InstanceData.DefaultRenderers["TextRenderer"];

            var size = _textRenderer.Font.MeasureString(Label);
            LocationComponent.Size = new Vector2(
                size.X + (2 * _slidingDoorRenderer.EndWidth),
                _slidingDoorRenderer.Height);
        }

        protected internal override void Render() {

            var size = LocationComponent.Size.ToPoint();

            _default = _slidingDoorRenderer.Render(size, 0);
            _hover = _slidingDoorRenderer.Render(size, 1);
            _pressed = _slidingDoorRenderer.Render(size, 2);

            _text = _textRenderer.Render(size, Label, TextHorizontal.CenterAligned, TextVertical.CenterAligned);
        }

        protected internal override Rectangle ChildRenderArea() {

            return LocationComponent.RenderBounds;
        }

        /*####################################################################*/
        /*                                Events                              */
        /*####################################################################*/

        protected internal override void MouseClick(MouseEventArgs e) {
            IsToggled = !IsToggled;
        }

        /*####################################################################*/
        /*                              Game Loop                             */
        /*####################################################################*/

        protected internal override void Update() { }

        protected internal override void Draw() {

            if (IsToggled) {
                SpriteBatch.Draw(_pressed, LocationComponent.RenderBounds, Color.White);
            } else if (IsHover) {
                SpriteBatch.Draw(_hover, LocationComponent.RenderBounds, Color.White);
            } else {
                SpriteBatch.Draw(_default, LocationComponent.RenderBounds, Color.White);
            }

            SpriteBatch.Draw(_text, LocationComponent.RenderLocation, Color.White);
        }        
    }
}